package tech.yunland.shop.domain.model;

import com.alibaba.fastjson.annotation.JSONField;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.CascadeType;
import org.hibernate.validator.constraints.Length;
import org.hibernate.validator.constraints.Range;
import tech.yunland.shop.domain.annotation.FilterFiled;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.OneToMany;
import javax.persistence.Transient;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;

/**
 * 商品
 *
 * @author 袁首京<yuanshoujing@gmail.com>
 */
@Entity
public class Goods extends BaseEntity {
    private static final long serialVersionUID = 6647568853903204120L;

    public static final String DJQ_ID = "3224b5f9-d073-40a9-8fbe-276b408c794e";
    public static final String JF_ID = "a7093a53-11d3-404c-bf3b-80ef7db398b5";

    @Column(length = 30)
    private String code; // 代码


    @FilterFiled(filterModel = "like")
    @Length(min = 1, max = 100)
    @Column(length = 100, nullable = false)
    private String name; // 名称
    @FilterFiled(filterModel = "eq")
    @Column(length = 100, nullable = false)
    private String spell; // 拼写

    @JSONField(serialize = false)
    @OneToMany(mappedBy = "goods")
    private Set<GoodsPictures> pictures; // 插图

    @Range(min = 1, max = 100)
    @Column(name = "kind_code", nullable = false)
    private Integer kindCode; // 商品类型

    @Column(length = 30)
    private String unit; // 计量单位

    @Column(nullable = false)
    private BigDecimal price = BigDecimal.ZERO; // 销售单价

    @Column(nullable = false)
    private BigDecimal prize = BigDecimal.ZERO; // 提成金额

    @Column(nullable = false)
    private BigDecimal score = BigDecimal.ZERO; // 积分单价

    @Column(nullable = false)
    private Long rate = 0l; // 权重

    @Range(min = -1, max = 1)
    @Column(nullable = false)
    private Integer state = 0; // 状态 -1 删除 0 未上架 1 上架

    @JSONField(serialize = false)
    @OneToMany(mappedBy = "goods")
    @Cascade(CascadeType.ALL)
    protected Set<TagGoods> tagGoods; // 商品标签
    
    @Transient
    private BigDecimal memberPrize = BigDecimal.ZERO;//店铺商品均价

	@Transient
    private String tagIds;

    @Transient
    private String tagNames;

    @Transient
    private String pictureId;

    @Transient
    private String pictureName;

    @JSONField(format = "yyyy-MM-dd hh:mm:ss")
    @Column(name = "create_time",nullable = false)
    private Date createTime = new Date();
    

    public BigDecimal getMemberPrize() {
		return memberPrize;
	}

	public void setMemberPrize(BigDecimal memberPrize) {
		this.memberPrize = memberPrize;
	}

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSpell() {
        return spell;
    }

    public void setSpell(String spell) {
        this.spell = spell;
    }

    public Set<GoodsPictures> getPictures() {
        return pictures;
    }

    public void setPictures(Set<GoodsPictures> pictures) {
        this.pictures = pictures;
    }

    public Integer getKindCode() {
        return kindCode;
    }

    public void setKindCode(Integer kindCode) {
        this.kindCode = kindCode;
    }

    public String getUnit() {
        return unit;
    }

    public void setUnit(String unit) {
        this.unit = unit;
    }

    public BigDecimal getPrice() {
        return price;
    }

    public void setPrice(BigDecimal price) {
        this.price = price;
    }

    public BigDecimal getPrize() {
        return prize;
    }

    public void setPrize(BigDecimal prize) {
        this.prize = prize;
    }

    public BigDecimal getScore() {
        return score;
    }

    public void setScore(BigDecimal score) {
        this.score = score;
    }

    public Long getRate() {
        return rate;
    }

    public void setRate(Long rate) {
        this.rate = rate;
    }

    public Integer getState() {
        return state;
    }

    public void setState(Integer state) {
        this.state = state;
    }

    public Set<TagGoods> getTagGoods() {
        return tagGoods;
    }

    public void setTagGoods(Set<TagGoods> tagGoods) {
        this.tagGoods = tagGoods;
    }

    public String getTagIds() {
        if (this.tagIds != null) {
            return this.tagIds;
        }

        if (this.tagGoods != null) {
            List ids = new ArrayList();
            for (TagGoods tagGoods : this.tagGoods) {
                ids.add(tagGoods.getTag().getId());
            }
            this.tagIds = StringUtils.join(ids, ",");
        }
        return this.tagIds;
    }

    public void setTagIds(String tagIds) {
        this.tagIds = tagIds;
    }

    public String getTagNames() {
        if (this.tagNames != null) {
            return this.tagNames;
        }

        if (this.tagGoods != null) {
            List names = new ArrayList();
            for (TagGoods tagGoods : this.tagGoods) {
                names.add(tagGoods.getTag().getName());
            }
            this.tagNames = StringUtils.join(names, ",");
        }
        return this.tagNames;
    }

    public void setTagNames(String tagNames) {
        this.tagNames = tagNames;
    }

    public String getPictureId() {
        if (this.pictureId != null) {
            return this.pictureId;
        }

        if (this.pictures != null) {
            List ids = new ArrayList();
            for (GoodsPictures goodsPictures : this.pictures) {
                ids.add(goodsPictures.getPicture().getId());
            }
            this.pictureId = StringUtils.join(ids, ",");
        }
        return this.pictureId;
    }

    public void setPictureId(String pictureId) {
        this.pictureId = pictureId;
    }

    //picture
    public String getPictureName() {
        if (this.pictureName != null) {
            return this.pictureName;
        }

        if (this.pictures != null) {
            List name = new ArrayList();
            for (GoodsPictures goodsPictures : this.pictures) {
                name.add(goodsPictures.getPicture().getFileName());
            }
            this.pictureName = StringUtils.join(name, ",");
        }
        return this.pictureName;
    }

    public void setPictureName(String PictureName) {
        this.pictureName = PictureName;
    }

    public Date getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }
}
